/**
 * 
 */
package com.desksoft.ecommerce.handler.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.desksoft.ecommerce.constants.Constants;
import com.desksoft.ecommerce.constants.ISession;
import com.desksoft.utility.cache.ICache;
import com.desksoft.utility.cache.IMemCacheController;
import com.desksoft.utility.cache.MemCachedUtil;
import com.desksoft.utility.cache.MemCachedUtil.UNIT;
import com.desksoft.utility.cache.NoneExistCacheServ;
import com.desksoft.utility.date.DateUtil;

/***************************************
 * 版权所有：杭州迪科软件 保留所有权利 创建日期: 创建作者：yejw 文件名称： 版本： 功能： 最后修改时间： 修改记录：
 *****************************************/
@SessionAttributes(ISession.SESSION_USER)
public class ControllerBase extends AbstractController implements IMemCacheController, ISession, Constants {

	protected final transient Log log = LogFactory.getLog(getClass());

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desksoft.ecommerce.handler.controller.AbstractController#
	 * getDateFormatPattern()
	 */
	@Override
	protected String getDateFormatPattern() {
		return DateUtil.yyyy_MM_DD_HH_MM_SS;
	}

	/**
	 * response 文档类型
	 */
	protected final static String CONTENT_TYPE_BMP = "image/bmp";
	protected final static String CONTENT_TYPE_GIF = "image/jpeg";
	protected final static String CONTENT_TYPE_TIFF = "image/tiff";
	protected final static String CONTENT_TYPE_DCX = "image/x-dcx";
	protected final static String CONTENT_TYPE_HTML = "text/html";
	protected final static String CONTENT_TYPE_PDF = "application/pdf";
	protected final static String CONTENT_TYPE_MSWORD = "application/msword";
	protected final static String CONTENT_TYPE_EXCEL = "application/vnd.ms-excel";
	protected final static String CONTENT_TYPE_FRAMEMAKER = "application/vnd.framemaker";

	/**
	 * memcached 缓存
	 */
	@Autowired
	private ICache memcache;

	/**
	 * 缓存KEY生成
	 * 
	 * @param clazz
	 * @param strs
	 * @return
	 */
	protected String getCacheKey(Class<?> clazz, String... strs) {
		StringBuilder sb = new StringBuilder(this.getCachePrefix(clazz));

		for (String s : strs) {
			sb.append(CACHE_SEP).append(s);
		}

		return sb.toString();
	}

	/**
	 * 缓存过期时间生成 单位分钟
	 * 
	 * @param cacheTime
	 * @return
	 */
	protected Date getCacheTime(int cacheTime) {
		return MemCachedUtil.getExpiry(UNIT.MINUTE, cacheTime);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.indoo.cache.IMemCache#getCachePrefix(java.lang.Class)
	 */
	@Override
	public String getCachePrefix(Class<?> clazz) {
		return CACHE_ACTION + MemCachedUtil.getCachePrefix(clazz);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.indoo.cache.IMemCache#getListCachePrefix(java.lang.Class)
	 */
	@Override
	public String getListCachePrefix(Class<?> clazz) {
		return CACHE_ACTION + MemCachedUtil.getListCachePrefix(clazz);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.indoo.cache.IMemCache#getMapCachePrefix(java.lang.Class)
	 */
	@Override
	public String getMapCachePrefix(Class<?> clazz) {
		return CACHE_ACTION + MemCachedUtil.getMapCachePrefix(clazz);
	}

	@Override
	public void setCache(ICache cache) {
		this.memcache = cache;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.desksoft.utility.cache.IMemCache#getCache()
	 */
	@Override
	public ICache getCache() {
		if (this.memcache == null) {
			return new NoneExistCacheServ();
		}
		return this.memcache;
	}

	/**
	 * 格式化时间类型
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat dateFormat = new SimpleDateFormat(DateUtil.yyyy_MM_DD_HH_MM_SS);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	/**
	 * 格式化时间类型
	 */
	@InitBinder
	public void initBinder2(WebDataBinder binder) {
		DateFormat dateFormat = new SimpleDateFormat(DateUtil.YYYY_MM_DD);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

}
